package yajco.fle.panels.printer;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 *
 * @author DeeL
 */
public abstract class PrinterPanel extends javax.swing.JPanel {

    /**
     * Creates new form PrinterPanel
     */
    public PrinterPanel() {
        initComponents();
    }
    
    public void printObject(Object o) {
        try {
            StringWriter stringWriter = new StringWriter();
            PrintWriter pw = new PrintWriter(stringWriter);
            printToWriter(o, pw);
            setText(stringWriter.toString());
        } catch (Exception e) {
            setText("ERROR: "+e.getMessage());
        }
    }
    
    protected void setText(String text) {
        printerTextArea.setText(text);
    }
    
    protected abstract void printToWriter(Object o, PrintWriter writer);

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrollPane = new javax.swing.JScrollPane();
        printerTextArea = new javax.swing.JTextArea();

        setBackground(new java.awt.Color(255, 255, 255));
        setPreferredSize(new java.awt.Dimension(400, 100));

        printerTextArea.setEditable(false);
        printerTextArea.setColumns(20);
        printerTextArea.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        printerTextArea.setRows(2);
        printerTextArea.setText("Printer output");
        scrollPane.setViewportView(printerTextArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollPane)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea printerTextArea;
    private javax.swing.JScrollPane scrollPane;
    // End of variables declaration//GEN-END:variables
}
